package leetcode100;

public class lc043 {
    public static void main(String[] args) {

    }

    public int minDistance(String word1, String word2) {
        int m = word1.length();
        int n = word2.length();

        int[][] dp = new int[m+1][n+1];

        for (int i = 0; i < m+1; i++) {
            dp[i][0] = i;
        }
        for (int i = 0; i < n+1; i++) {
            dp[0][i] = i;
        }

        for (int i = 0; i < m+1; i++) {
            for (int j = 0; j < n+1; j++) {
                if (word1.charAt(i-1)==word2.charAt(j-1)) {
                    dp[i][j] = dp[i-1][j-1];
                } else {
                    dp[i][j] = Math.min(Math.min(dp[i-1][j],//删除,i相比i-1多了一个字母,达到j字母,需要多删除一次
                            dp[i][j-1]),//增加,j多了一个,原来i->j-1个字母的基础上多增加一个字母
                            dp[i-1][j-1])+1;//替换,i和j同时加1 个字母
                }


            }
        }

        return dp[m][n];
    }
}
